import { get_gun, clear_gun } from 'base/api';
import { put_log } from 'base/ctl/log';
import { g_db } from 'db/state';
import { do_event } from './event';
import { init_info } from 'use/info';
import { d_sort, uDb, uReal, obj_int, cline, obj_str } from 'sui';

const ck_box = (box_sn) => {
    const herr = (e) => {
        return `箱子编码错误: ${e}`;
    };

    const tmp = box_sn.split("-");
    if (tmp.length !== 3 || tmp[1].length < 2 || tmp[2].length !== 3) {
        return herr("位数错误");
    }

    if (tmp[0] !== "TX") {
        return herr("开头必须为TX");
    }

    const c = tmp[1][0];
    if (c !== "R" && c !== "G" && c !== "B") {
        return herr("第二部分开头必须为R, G, B");
    }

    const rgb = parseInt(tmp[1].slice(1), 10);
    if (isNaN(rgb) || rgb > 255 || rgb < 0) {
        return herr("第二部分数字位于 0 到 255");
    }

    return null;

};

const do_box = (stepnum, step, umd, watch) => {
    const box_sn = get_gun(step);
    if (!box_sn) {
        watch.startWatch();
        return;
    }

    clear_gun();

    umd.mset({
        "box": box_sn,
        "err": "",
    });

    put_log("编码", box_sn, "扫描箱子编码");

    const herr = (e) => {
        umd.setErr(e);
        watch.startWatch();
    };

    const e = ck_box(box_sn);
    if (e) {
        return herr(e);
    }

    uReal(null, "pair/addbox",
        {
            line: cline(),
            k: box_sn,
        },
        r => {
            let attr = null;
            const box = [];

            r.forEach(d => {
                if (d.sn === "attr") {
                    attr = d;
                    return;
                }

                box.push(d);
            });

            init_info(box);

            const c = box.length;

            umd.set("sum", c);
            g_db.s_sum(c);

            g_db.s_box(box_sn);
            g_db.s_boxtp(obj_str(attr, "tp"));
            g_db.s_task(obj_str(attr, "task"));
            g_db.s_boxtray(obj_int(attr, "tray"));
            g_db.s_boxlv(obj_int(attr, "level"));

            do_event("step_over", umd, watch);
        },
        e => herr("添加料箱出错"));
};

const do_box_data = (stepnum, step, umd, watch) => {
    const box_sn = get_gun(step);
    if (!box_sn) {
        watch.startWatch();
        return;
    }

    clear_gun();

    umd.mset({
        "box": box_sn,
        "err": "",
    });

    put_log("编码", box_sn, "扫描箱子编码");

    const herr = (e) => {
        umd.setErr(e);
        watch.startWatch();
    };

    const e = ck_box(box_sn);
    if (e) {
        return herr(e);
    }

    uDb(null, `file/list/box_${box_sn}`, { line: cline() },
        r => {
            const box = [];

            r.forEach(d => {
                if (d.sn !== "attr") {
                    box.push(d);
                }
            });

            d_sort(box, "pos", true);
            umd.setInfo(box);

            g_db.s_box(box_sn);

            do_event("step_over", umd, watch);
        },
        e => herr("箱中没有数据"));
};

const do_boxpsn = (stepnum, step, umd, watch) => {
    const box_sn = get_gun(step);
    if (!box_sn) {
        watch.startWatch();
        return;
    }

    clear_gun();

    umd.mset({
        "box": box_sn,
        "err": "",
    });

    put_log("编码", box_sn, "扫描箱子编码");

    const herr = (e) => {
        umd.setErr(e);
        watch.startWatch();
    };

    const e = ck_box(box_sn);
    if (e) {
        return herr(e);
    }

    uDb(null, `file/list/box_${box_sn}`, { line: cline() },
        r => {
            let attr = null;
            const box = [];

            r.forEach(d => {
                if (d.sn === "attr") {
                    attr = d;
                    return;
                }

                box.push(d);
            });

            init_info(box);

            const c = box.length;

            umd.set("sum", c);
            g_db.s_sum(c);

            g_db.s_box(box_sn);
            g_db.s_boxtp(obj_str(attr, "tp"));

            watch.startWatch();
        },
        e => herr("料箱出错"));
};

const do_boxscan = (stepnum, step, umd, watch) => {
    const box_sn = get_gun(step);
    if (!box_sn) {
        watch.startWatch();
        return;
    }

    clear_gun();

    umd.mset({
        "box": box_sn,
        "err": "",
    });

    put_log("编码", box_sn, "扫描箱子编码");

    const herr = (e) => {
        umd.setErr(e);
        watch.startWatch();
    };

    const e = ck_box(box_sn);
    if (e) {
        return herr(e);
    }

    uDb(null, `file/list/box_${box_sn}`, { line: cline() },
        r => {
            const box = [];
            let task = "";

            r.forEach(d => {
                if (d.sn === "attr") {
                    task = obj_str(d, "task");
                    return;
                }

                box.push(d);
            });

            if (!task) {
                return herr("箱子没有绑定工单");
            }

            d_sort(box, "pos", true);

            uDb(null, "task/get",
                {
                    line: cline(),
                    k: task,
                },
                r_task => {
                    init_info(box);

                    const c = box.length;
                    g_db.s_sum(c);

                    const tech = obj_str(r_task, "tech");

                    umd.mset({
                        "sum": c,
                        "task": task,
                        "tech": tech,
                    });

                    umd.setPara(r_task);

                    g_db.s_box(box_sn);
                    g_db.s_tech(tech);
                    g_db.s_task(task);

                    watch.startWatch();
                }, e => herr("没有得到工单信息"));
        },
        e => herr("箱中没有数据"));
};

export {do_boxscan, do_boxpsn, do_box_data, do_box, ck_box};
